package com.shoujifeng.snowmusic.player.http;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.httpclient.HttpException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;

public class ImageAccess
{
	public static final int BUFFER_SIZE = 8192;
	
	
	private final String SUFFIX	= ".jpg";

	public byte[] getImageForNet(String pathString, String savePath) throws Exception
	{
		if(pathString.indexOf(SUFFIX) == -1)	pathString += SUFFIX;
		if(savePath.indexOf(SUFFIX) == -1) savePath += SUFFIX;
		URL url = new URL(pathString);

		HttpURLConnection conn = (HttpURLConnection) url.openConnection();

		conn.setRequestMethod("GET");

		conn.setConnectTimeout(15000);

		if (conn.getResponseCode() == 200)
		{
			InputStream inputStream = conn.getInputStream();

			return saveInputStreamReByte(inputStream, savePath);
		}

		return null;
	}

	/**
	 * Save InputStream to disk
	 * 
	 * @throws FileNotFoundException
	 * @throws HttpException
	 * @throws IOException
	 */
	public static void saveInputStreamToFile(InputStream in, String filename)
			throws FileNotFoundException, HttpException, IOException
	{
		BufferedOutputStream out = null;
		byte[] bt = new byte[BUFFER_SIZE];

		try
		{
			File file = new File(filename);
			if (!file.getParentFile().exists())
			{
				file.getParentFile().mkdirs();
			}
			out = new BufferedOutputStream(new FileOutputStream(file));

			int readLength = in.read(bt);
			while (readLength != -1)
			{
				out.write(bt, 0, readLength);
				readLength = in.read(bt);
			}
		} finally
		{
			if (in != null)
			{
				try
				{
					in.close();
				} catch (Exception e)
				{
				}
			}
			if (out != null)
			{
				try
				{
					out.close();
				} catch (Exception e)
				{
				}
			}
		}
	}
	
	/**
	 * Save InputStream to disk
	 * 
	 * @throws FileNotFoundException
	 * @throws HttpException
	 * @throws IOException
	 */
	public static byte[] saveInputStreamReByte(InputStream in, String filename)
			throws FileNotFoundException, HttpException, IOException {
		BufferedOutputStream out = null;
		byte[] bt = new byte[BUFFER_SIZE];

		try {
			File file = new File(filename);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			out = new BufferedOutputStream(new FileOutputStream(file));
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			
			int readLength = in.read(bt);
			while (readLength != -1) {
				out.write(bt, 0, readLength);
				outStream.write(bt, 0, readLength);
				readLength = in.read(bt);
			}
	
			return outStream.toByteArray();
		} finally {
			if (in != null) {
				try {in.close();} catch (Exception e) {}
			}
			if (out != null) {
				try {out.close();} catch (Exception e) {}
			}
		}
	
	}
	
	/**
	 * 获取本地图片
	 * @param pathString
	 * @return
	 * @throws IOException
	 */
	public BitmapDrawable getImageDrawable(String pathString) throws IOException
	{
		String path = pathString;
		if(path.indexOf(".jpg") == -1)
		{
			path += ".jpg";
		}
		
		//检查文件是否存在
		File file = new File(path);
		if (!file.exists())
		{
			return null;
		}

		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] bt = new byte[BUFFER_SIZE];

		InputStream in = new FileInputStream(file);

		int readLength = in.read(bt);
		while (readLength != -1)
		{
			outStream.write(bt, 0, readLength);
			readLength = in.read(bt);
		}

		byte[] data = outStream.toByteArray();
		Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
		BitmapDrawable bd = new BitmapDrawable(bitmap);

		return bd;
	}
}
